<?php
namespace App\Models;

use App\Services\CommonService;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class OrderDepict extends Model
{
    use HasFactory;

    protected $table = 'sp_order_deposit';
    protected $primaryKey = 'id';
    public $timestamps = false;
    /**
     * @var mixed
     */
    private $order_id;
    /**
     * @var mixed
     */
    private $user_id;
    /**
     * @var mixed
     */
    private $amount;

    public function order()
    {
        return $this->belongsTo(Order::class, 'order_id', 'id');
    }
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function getDepict()
    {
        return $this->hasMany(OrderDepict::class, 'order_id', 'id');
    }

    public function createOrderDepict($orderDepict): bool
    {
       return $this->query()->insert($orderDepict);
    }

    //审核列表
    public  function checkList($request){
        $pageSize = !empty($request['pageSize'])?$request['pageSize']:CommonService::PAGESIZE;
        $where=[];
        $check_where=[];

        if(!empty($request['trade_no'])){
            $where[] = ['o.trade_no','like','%'.trim($request['trade_no']).'%'];
        }
        if(!empty($request['alipay_trade_no'])){
            $where[] = ['o.alipay_trade_no','like','%'.trim($request['alipay_trade_no']).'%'];
        }
        if(!empty($request['mobile'])){
            $where[] = ['m.mobile','=',trim($request['mobile'])];
        }
        if(!empty($request['name'])){
            $where[] = ['m.cert_name','=',trim($request['name'])];
        }
        if($request['check_status']!=''){
            $check_where[] = ['od.status',$request['check_status']];
        }
        if($request['refund_screen_status']!=''){
            $check_where[] = ['od.refund_screen_status',$request['refund_screen_status']];
        }
        if($request['refund_deposit_status']!=''){
            $check_where[] = ['od.refund_screen_status',$request['refund_screen_status']];
        }
        if(!empty($request['create_time'])){
            $time_data = searchTime($request['create_time']);
            $begin_time = $time_data['begin_time'];
            $end_time = $time_data['end_time'];
            $where[] = ['od.create_time','>=',strtotime($begin_time)];
            $where[] = ['od.create_time','<=',strtotime($end_time)];
        }

        $list = $this->from('sp_order_deposit as od')->where($where)->where($check_where)
            ->leftjoin('sp_order_2 as o','o.id','=','od.order_id')
            ->leftjoin('sp_member_2 as m','m.id','=','od.user_id')
            ->select('o.trade_no','m.mobile',
                'm.cert_name','od.*')
            ->orderBy('od.id', 'desc')
            ->paginate($pageSize);

        unset($check_where);
        $check_where[] = ['status','>=',0];

        $all_audit = $this->from('sp_order_deposit')->where($check_where)->count();

        unset($check_where);
        $check_where[] = ['status',0];

        $nocheck_count = $this->from('sp_order_deposit')->where($check_where)->count();

        unset($check_where);
        $check_where[] = ['status',1];

        $checked_count = $this->from('sp_order_deposit')->where($check_where)->count();

        unset($check_where);
        $check_where[] = ['status',2];
        $refund_count = $this->from('sp_order_deposit')->where($check_where)->count();


        $list = $list ? $list->toArray() : array();
        $list['count']['all_audit'] = $all_audit;
        $list['count']['nocheck_count'] = $nocheck_count;
        $list['count']['checked_count'] = $checked_count;
        $list['count']['refund_count'] = $refund_count;

        return $list;
    }

}
